﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class ManPowerResourceBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IManPowerResourceDA manpowerResourceDA = DataAccess.CreateManPowerResourceDA();
        private PubParasInfo pubParasInfo;

        public ManPowerResourceBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ManPowerResourceBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> orderIDS)
        {
            string temp = string.Empty;
            int index = 0;
            string order = string.Empty;
            List<string> orderType = new List<string>();
            List<string> orderID = new List<string>();
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < orderIDS.Count; i++)
                {
                    index = orderIDS[i].IndexOf(";");
                    orderType.Add(orderIDS[i].Substring(0, index));
                    orderID.Add(orderIDS[i].Substring(index + 1, (orderIDS[i].Length - index) - 1));
                    order = order + orderIDS[i].Substring(index + 1, (orderIDS[i].Length - index) - 1) + ";";
                }
                if (manpowerResourceDA.DeleteManPowerResource(dataBase, tran, orderID, orderType, this.dataBaseID[0]) > 0)
                {
                    temp = order;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return manpowerResourceDA.GetAllManPowerResource(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string orderIDS)
        {
            int index = orderIDS.IndexOf(";");
            string orderType = orderIDS.Substring(0, index);
            string orderID = orderIDS.Substring(index + 1, (orderIDS.Length - index) - 1);
            return manpowerResourceDA.GetManPowerResourceByID(orderID, orderType, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            return null;
        }

        public string Insert(object obj)
        {
            string orderID = string.Empty;
            try
            {
                ManPowerResourceInfo manpowerResourceInfo = obj as ManPowerResourceInfo;
                manpowerResourceInfo.CreatedDate = DateTime.Now;
                manpowerResourceInfo.UpdatedDate = DateTime.Now;
                manpowerResourceInfo.UpdatedBy = manpowerResourceInfo.CreatedBy;
                if (manpowerResourceDA.InsertManPowerResource(manpowerResourceInfo, this.dataBaseID[0]) > 0)
                {
                    orderID = manpowerResourceInfo.OrderID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return orderID;
        }

        public string Update(object obj)
        {
            string orderID = string.Empty;
            try
            {
                ManPowerResourceInfo manpowerResourceInfo = obj as ManPowerResourceInfo;
                if (manpowerResourceDA.CheckManPowerResourceUnique(manpowerResourceInfo.OrderID, manpowerResourceInfo.OrderType, this.dataBaseID[0]))
                {
                    manpowerResourceInfo.UpdatedDate = DateTime.Now;
                    if (manpowerResourceDA.UpdateManPowerResource(manpowerResourceInfo, this.dataBaseID[0]) > 0)
                    {
                        orderID = manpowerResourceInfo.OrderID;
                    }
                    return orderID;
                }
                manpowerResourceInfo.CreatedDate = DateTime.Now;
                manpowerResourceInfo.UpdatedDate = DateTime.Now;
                manpowerResourceInfo.UpdatedBy = manpowerResourceInfo.CreatedBy;
                if (manpowerResourceDA.InsertManPowerResource(manpowerResourceInfo, this.dataBaseID[0]) > 0)
                {
                    orderID = manpowerResourceInfo.OrderID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return orderID;
        }
    }
}

